package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.EbookEntry;
import com.qf.mapper.EbookEntryMapper;
import com.qf.service.EbookEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ASUS
 * @version V1.0
 * @Project work
 * @Package com.qf.service.impl
 * @Date 2021/10/2 16:32
 */
@Service
public class EbookEntryServiceImpl implements EbookEntryService {

    @Autowired
    private EbookEntryMapper ebookEntryMapper;

    @Override
    public PageInfo<EbookEntry> findAllByPage(String category, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<EbookEntry> list = ebookEntryMapper.findAllByPage(category);
        PageInfo<EbookEntry> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void insert(EbookEntry ebookEntry) {
        EbookEntry byId = findById(ebookEntry.getId());
        if(byId != null){
            throw new RuntimeException("添加失败，书籍已存在");
        }
        int i = ebookEntryMapper.insertSelective(ebookEntry);
        if(i != 1){
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void update(EbookEntry ebookEntry) {
        EbookEntry byId = findById(ebookEntry.getId());
        if(byId == null){
            throw new RuntimeException("修改失败，书籍不存在");
        }
        int i = ebookEntryMapper.updateByPrimaryKeySelective(ebookEntry);
        if(i != 1){
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public void delete(long id) {
        int i = ebookEntryMapper.deleteByPrimaryKey(id);
        if(i != 1){
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public EbookEntry findById(long id) {
        return ebookEntryMapper.selectByPrimaryKey(id);
    }
}
